package com.rover.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grid.Page;
import com.rover.entity.system.User;
import com.rover.entity.system.UserLog;
import com.rover.service.system.UserLogService;
import com.rover.service.system.UserService;
import com.utils.AppUtil;


/**
 * 公开访问
 */
@Controller
@RequestMapping("/userLogController")
public class UserLogController {
	
    @Autowired
    private UserLogService userLogService;
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	Map<String, Object> map = new HashMap<String, Object>();
    	String loginName = (String) request.getParameter("loginName");
    	String hql = "";
		if(StringUtils.isNotBlank(loginName)){
			hql = "where loginName like '%" + loginName + "%'";
		}
		hql = "from User " + hql;
		List<User> users = userService.findByHql(hql);
		List<Long> ids = new ArrayList<Long>();
		for(User user : users){
			ids.add(user.getId());
		}
		Map<String, Object> params = new HashMap<String, Object>();
		Page<UserLog> page = new Page<UserLog>(request);
		page = userLogService.query(page, params, users, ids, hql);
		map = AppUtil.buildJSONDataResponse(page.getResult(),
				page.getTotalCount());
		return map;
    }
    
}
